package com.rjpacket.chileme.app.adapters;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.rjpacket.chileme.app.R;
import com.rjpacket.chileme.app.models.CateenInfoIcon;
import com.rjpacket.chileme.app.models.Restaurant;
import com.rjpacket.chileme.app.tasks.StoreIconTask;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WIN7 on 2015/3/23.
 */

/**
 * 商铺列表的Adapter
 */
public class CanteenListAdapter extends BaseAdapter {

    private List<Restaurant> data;
    private LayoutInflater inflater;

    public CanteenListAdapter(List<Restaurant> data, Context context) {
        if (context != null) {
            inflater = LayoutInflater.from(context);
        } else {
            throw new IllegalArgumentException("Context must be set.");
        }
        this.data = data;
    }


    @Override
    public int getCount() {
        int ret = 0;
        if (data != null) {
            ret = data.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        Object ret = null;
        if (data != null) {
            ret = data.get(position);
        }
        return ret;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View ret = null;
        if (convertView != null) {
            ret = convertView;
        } else {
            ret = inflater.inflate(R.layout.mz_item_store, parent, false);
        }

        ViewHolder holder = (ViewHolder) ret.getTag();
        if (holder == null) {
            holder = new ViewHolder();

            holder.storeIcon = (ImageView) ret.findViewById(R.id.store_icon);
            holder.storeName = (TextView) ret.findViewById(R.id.store_name);
            holder.storeTips = (TextView) ret.findViewById(R.id.store_tips);
            holder.storeDivider = (TextView) ret.findViewById(R.id.store_divider);

            holder.storeDiscountFirst = (TextView) ret.findViewById(R.id.store_description_first);
            holder.storeDiscountSecond = (TextView) ret.findViewById(R.id.store_description_second);

            holder.storeIconIndexFirst = (TextView) ret.findViewById(R.id.store_icon_first);
            holder.storeIconIndexSecond = (TextView) ret.findViewById(R.id.store_icon_second);
            holder.storeIconIndexThird = (TextView) ret.findViewById(R.id.store_icon_horizontal_first);
            holder.storeIconIndexFourth = (TextView) ret.findViewById(R.id.store_icon_horizontal_second);
            holder.storeIconIndexFifth = (TextView) ret.findViewById(R.id.store_icon_horizontal_third);

            ret.setTag(holder);
        }

        String str = null;

        Restaurant restaurant = data.get(position);
        //商铺提示信息
        str = restaurant.getTips();
        holder.storeTips.setText(str);
        //商铺名称
        str = restaurant.getName();
        holder.storeName.setText(str);

        //获得店铺图标网址
        String image_url = restaurant.getImage_url();
        ImageView imageView = holder.storeIcon;
        //执行下载任务
        //TODO 防止图片错位
        StoreIconTask task = new StoreIconTask(imageView);
        task.execute(image_url);

        //商铺活动图标
        ArrayList<CateenInfoIcon> icons = restaurant.getIcons();
        //如果第一个图标的index>2  就隐藏分割线
        if (icons.get(0).getIndex()>2){
            holder.storeDivider.setVisibility(View.GONE);
        }
        for (CateenInfoIcon icon : icons) {
            int index = icon.getIndex();

            int r, g, b;
            String[] color;
            String text;
            String id;

            //根据图标的index选择让对应控件显示
            switch (index) {
                case 1:
                    color = icon.getColor().split(",");
                    r = Integer.parseInt(color[0]);
                    g = Integer.parseInt(color[1]);
                    b = Integer.parseInt(color[2]);
                    text = icon.getText();
                    id = icon.getId();
                    holder.storeIconIndexFirst.setVisibility(View.VISIBLE);
                    holder.storeIconIndexFirst.setBackgroundColor(Color.rgb(r, g, b));
                    holder.storeIconIndexFirst.setText(id);

                    holder.storeDiscountFirst.setVisibility(View.VISIBLE);
                    holder.storeDiscountFirst.setText(text);
                    break;
                case 2:
                    color = icon.getColor().split(",");
                    r = Integer.parseInt(color[0]);
                    g = Integer.parseInt(color[1]);
                    b = Integer.parseInt(color[2]);
                    text = icon.getText();
                    id = icon.getId();

                    holder.storeIconIndexSecond.setVisibility(View.VISIBLE);
                    holder.storeIconIndexSecond.setText(id);
                    holder.storeIconIndexSecond.setBackgroundColor(Color.rgb(r, g, b));

                    holder.storeDiscountSecond.setVisibility(View.VISIBLE);
                    holder.storeDiscountSecond.setText(text);

                    break;
                case 3:
                    color = icon.getColor().split(",");

                    r = Integer.parseInt(color[0]);
                    g = Integer.parseInt(color[1]);
                    b = Integer.parseInt(color[2]);
                    id = icon.getId();

                    holder.storeIconIndexThird.setVisibility(View.VISIBLE);
                    holder.storeIconIndexThird.setText(id);
                    holder.storeIconIndexThird.setBackgroundColor(Color.rgb(r,g,b));

                    break;
                case 4:
                    color = icon.getColor().split(",");
                    r = Integer.parseInt(color[0]);
                    g = Integer.parseInt(color[1]);
                    b = Integer.parseInt(color[2]);
                    id = icon.getId();

                    holder.storeIconIndexFourth.setVisibility(View.VISIBLE);
                    holder.storeIconIndexFourth.setText(id);
                    holder.storeIconIndexFourth.setBackgroundColor(Color.rgb(r,g,b));

                    break;
                case 5:
                    color = icon.getColor().split(",");
                    r = Integer.parseInt(color[0]);
                    g = Integer.parseInt(color[1]);
                    b = Integer.parseInt(color[2]);
                    id = icon.getId();

                    holder.storeIconIndexFifth.setVisibility(View.VISIBLE);
                    holder.storeIconIndexFifth.setText(id);
                    holder.storeIconIndexFifth.setBackgroundColor(Color.rgb(r,g,b));

                    break;
            }
        }
        return ret;
    }

    public class ViewHolder {

        public ImageView storeIcon;
        public TextView storeName;
        //商铺销量/起送价/耗时的描述
        public TextView storeTips;
        public TextView storeDivider;
        //商铺活动的图标
        public TextView storeIconIndexFirst;
        public TextView storeIconIndexSecond;
        public TextView storeIconIndexThird;
        public TextView storeIconIndexFourth;
        public TextView storeIconIndexFifth;
        //活动的描述
        public TextView storeDiscountFirst;
        public TextView storeDiscountSecond;
    }


}
